package ha.dal.dao.test;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Charsets;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import com.google.common.io.Files;
import com.smartwebx.ha.dal.assign.dao.HaAdminAreaMapper;
import com.smartwebx.ha.dal.usebean.StationAdminArea;

public class AdminAreaCode extends AbstractDataAccessTests {
	@Autowired
	private HaAdminAreaMapper haStationAreaDao;

	@Test
	public void insert() throws IOException {
		List<String> lines = Files.readLines(new File("d:/1.txt"),
				Charsets.UTF_8);
		// List<StationAdminArea> adminAreas = Lists.newArrayList();

		String parentId = null;

		String oneLevelId = null;
		String twoLevelId = null;
		for (String line : lines) {
			int findKey = StringUtils.countMatches(line, "\t");
			Iterable<String> lineIter = Splitter.on('\t').omitEmptyStrings()
					.split(line);
			String id = Iterables.get(lineIter, 0);
			String name = Iterables.get(lineIter, 1);

			if (findKey == 1) {
				oneLevelId = id;
				parentId = "0";
			} else if (findKey == 2) {
				twoLevelId = id;
				parentId = oneLevelId;
			} else if (findKey == 3) {
				parentId = twoLevelId;
			}

			StationAdminArea stationAdminArea = new StationAdminArea(id,
					parentId, name, findKey);

			haStationAreaDao.insert(stationAdminArea);
		}

	}
}
